package gov.pbc.nn.kjgl.AbroadApproval.dao;

import gov.pbc.nn.core.bdf.IUserSelect;
import gov.pbc.nn.core.bdf.service.UserService.DefaultUserRowMapper;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import gov.pbc.nn.kjgl.Constants;
@Component("userSelectBWDao")
public class UserSelectBWDao implements IUserSelect {
	
	@Resource(name=PbcnnCoreJdbcDao.BEAN_ID)
	private PbcnnCoreJdbcDao jdbcDao;
	

	@Override
	public void loadUsers(Page<IUser> page, Criteria criteria,
		Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
//		String roleName = "科长";
		String roleName2 = Constants.ROLE_CZ;
		String deptId = "489145101058";
		String sql =  "SELECT\n"
				+ "	U.*\n"
				+ "FROM\n"
				+ "	T_USER U\n"
				+ "INNER JOIN BDF2_ROLE_MEMBER RM ON U.USERNAME_ = RM.USERNAME_\n"
				+ "INNER JOIN BDF2_ROLE R ON RM.ROLE_ID_ = R.ID_\n"
				+ "INNER JOIN BDF2_USER_DEPT UD ON UD.USERNAME_ = U.USERNAME_\n"
				+ "WHERE\n"
				+ "R.NAME_ = :roleName2\n"
				+ "AND UD.DEPT_ID_ = :deptId";
		ParseResult result = jdbcDao.parseCriteria(criteria, true, "T");
		if (result != null) {
			StringBuffer sb = result.getAssemblySql();
			Map<String, Object> valueMap = result.getValueMap();
			sql += sb.toString();
			//valueMap.put("roleName", roleName);
			valueMap.put("roleName2", roleName2);
			valueMap.put("deptId", deptId);
			// this.pagingQuery(page, sql, valueMap.values().toArray(),
			// new DefaultUserRowMapper());
			jdbcDao.pagingQuery(page, sql, new DefaultUserRowMapper(), valueMap);
		} else {
			Map<String,Object> mparams = new HashMap<String,Object>();
			//mparams.put("roleName", roleName);
			mparams.put("roleName2", roleName2);
			mparams.put("deptId", deptId);
			jdbcDao.pagingQuery(page, sql, new DefaultUserRowMapper(), mparams);
//			this.pagingQuery(page, sql, new Object[] { deptId },
//					new DefaultUserRowMapper());
		}

	}

}







